<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工位名称" prop="gwName">
        <el-input
          v-model="queryParams.gwName"
          placeholder="请输入工位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料编码" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入物料编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品编码" prop="cpbm">
        <el-input
          v-model="queryParams.cpbm"
          placeholder="请输入产品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:techDetail:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:techDetail:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:techDetail:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:techDetail:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >导入
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="techDetailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="公司代码" align="center" prop="gcCode"/>
      <el-table-column label="公司名称" align="center" prop="gcName"/>
      <el-table-column label="产品编码" align="center" prop="cpbm"/>
      <el-table-column label="产品名称" align="center" prop="productName"/>
      <el-table-column label="规格型号" align="center" prop="specModel"/>
      <el-table-column label="线体类别" align="center" prop="lineType" width="140">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.line_type" :value="scope.row.lineType"/>
        </template>
      </el-table-column>
      <el-table-column label="生产线编码" align="center" prop="lineCode"/>
      <el-table-column label="生产线名称" align="center" prop="lineName"/>
      <el-table-column label="车间编码" align="center" prop="cjCode"/>
      <el-table-column label="车间名称" align="center" prop="cjName"/>
      <el-table-column label="产线物料编码" align="center" prop="lineItemCode"/>
      <el-table-column label="产线物料名称" align="center" prop="lineItemName"/>
      <el-table-column label="工位名称" align="center" prop="gwName"/>
      <el-table-column label="工位编码" align="center" prop="gwCode"/>
      <el-table-column label="物料编码" align="center" prop="itemCode"/>
      <el-table-column label="物料名称" align="center" prop="itemName"/>
      <el-table-column label="物料规格" align="center" prop="bwlSpec"/>
      <el-table-column label="单位" align="center" prop="unit"/>
      <el-table-column label="单台用量" align="center" prop="dtyl"/>
      <el-table-column label="单件重量" align="center" prop="djzl"/>
      <el-table-column label="单件用时" align="center" prop="djys"/>
      <el-table-column label="单件人工" align="center" prop="djrg"/>
      <el-table-column label="物料来源" align="center" prop="wlly"/>
      <el-table-column label="加工工艺" align="center" prop="jggy"/>
      <el-table-column label="前工位" align="center" prop="qgw"/>
      <el-table-column label="后工位" align="center" prop="hgw"/>
      <el-table-column label="相邻工位" align="center" prop="xlgw"/>
      <el-table-column label="移位时间(秒)" align="center" prop="ywsj" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ywsj, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工位顺序" align="center" prop="gwsx"/>
      <el-table-column label="工位说明" align="center" prop="gwsm"/>
      <el-table-column label="关联目的地" align="center" prop="glmdd"/>
      <el-table-column label="关联来源" align="center" prop="glly"/>
      <el-table-column label="生产缓存区" align="center" prop="schcq"/>
      <el-table-column label="线边库代码" align="center" prop="xbkdm"/>
      <el-table-column label="线边库名称" align="center" prop="xbkmc"/>
      <el-table-column label="上线仓库编码" align="center" prop="sxckbm"/>
      <el-table-column label="上线仓库名称" align="center" prop="sxckmc"/>
      <el-table-column label="上线单元控制" align="center" prop="sxdykz"/>
      <el-table-column label="拉动方式" align="center" prop="ldWays"/>
      <el-table-column label="拉动控制时间" align="center" prop="ldTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ldTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="拉动地址" align="center" prop="ldAddress"/>
      <el-table-column label="PC仓库代码" align="center" prop="pcWarehouseCode"/>
      <el-table-column label="PC仓库名称" align="center" prop="pcWarehouseName"/>
      <el-table-column label="退料仓库代码" align="center" prop="tlWarehouseCode"/>
      <el-table-column label="退料仓库名称" align="center" prop="tlWarehouseName"/>
      <el-table-column label="生产线简码" align="center" prop="proSimpleCode"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:techDetail:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:techDetail:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工艺明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="工位名称" prop="gwName">
              <el-input v-model="form.gwName" placeholder="请输入工位名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="工位编码" prop="gwCode">
              <el-input v-model="form.gwCode" placeholder="请输入工位编码"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="规格型号" prop="specModel">
              <el-input v-model="form.specModel" placeholder="请输入规格型号"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="生产线编码" prop="lineCode">
              <el-input v-model="form.lineCode" placeholder="请输入生产线编码"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="生产线名称" prop="lineName">
              <el-input v-model="form.lineName" placeholder="请输入生产线名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="物料编码" prop="itemCode">
              <el-input v-model="form.itemCode" placeholder="请输入物料编码"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="物料名称" prop="itemName">
              <el-input v-model="form.itemName" placeholder="请输入物料名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="部物料规格" prop="bwlSpec">
              <el-input v-model="form.bwlSpec" placeholder="请输入部物料规格"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单台用量" prop="dtyl">
              <el-input v-model="form.dtyl" placeholder="请输入单台用量"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="单件重量" prop="djzl">
              <el-input v-model="form.djzl" placeholder="请输入单件重量"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单件用时" prop="djys">
              <el-input v-model="form.djys" placeholder="请输入单件用时"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单件人工" prop="djrg">
              <el-input v-model="form.djrg" placeholder="请输入单件人工"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="物料来源" prop="wlly">
              <el-input v-model="form.wlly" placeholder="请输入物料来源"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="加工工艺" prop="jggy">
              <el-input v-model="form.jggy" placeholder="请输入加工工艺"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="前工位" prop="qgw">
              <el-input v-model="form.qgw" placeholder="请输入前工位"/>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="8">
            <el-form-item label="后工位" prop="hgw">
              <el-input v-model="form.hgw" placeholder="请输入后工位"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="相邻工位" prop="xlgw">
              <el-input v-model="form.xlgw" placeholder="请输入相邻工位"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="移位时间(秒)" prop="ywsj">
              <el-date-picker clearable
                              v-model="form.ywsj"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择移位时间(秒)">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="工位顺序" prop="gwsx">
              <el-input v-model="form.gwsx" placeholder="请输入工位顺序"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="工位说明" prop="gwsm">
              <el-input v-model="form.gwsm" placeholder="请输入工位说明"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="关联目的地" prop="glmdd">
              <el-input v-model="form.glmdd" placeholder="请输入关联目的地"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="关联来源" prop="glly">
              <el-input v-model="form.glly" placeholder="请输入关联来源"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="生产缓存区" prop="schcq">
              <el-input v-model="form.schcq" placeholder="请输入生产缓存区"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="线边库代码" prop="xbkdm">
              <el-input v-model="form.xbkdm" placeholder="请输入线边库代码"/>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="8">
            <el-form-item label="产品编码" prop="cpbm">
              <el-input v-model="form.cpbm" placeholder="请输入产品编码"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="线边库名称" prop="xbkmc">
              <el-input v-model="form.xbkmc" placeholder="请输入线边库名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="上线仓库编码" prop="sxckbm">
              <el-input v-model="form.sxckbm" placeholder="请输入上线仓库编码"/>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="8">
            <el-form-item label="上线仓库名称" prop="sxckmc">
              <el-input v-model="form.sxckmc" placeholder="请输入上线仓库名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="上线单元控制" prop="sxdykz">
              <el-input v-model="form.sxdykz" placeholder="请输入上线单元控制"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="拉动方式" prop="ldWays">
              <el-input v-model="form.ldWays" placeholder="请输入拉动方式"/>
            </el-form-item>
          </el-col>
        </el-row>



        <el-row>
          <el-col :span="8">
            <el-form-item label="拉动控制时间" prop="ldTime">
              <el-date-picker clearable
                              v-model="form.ldTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择拉动控制时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="拉动地址" prop="ldAddress">
              <el-input v-model="form.ldAddress" placeholder="请输入拉动地址"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="PC仓库代码" prop="pcWarehouseCode">
              <el-input v-model="form.pcWarehouseCode" placeholder="请输入PC仓库代码"/>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="8">
            <el-form-item label="PC仓库名称" prop="pcWarehouseName">
            <el-input v-model="form.pcWarehouseName" placeholder="请输入PC仓库名称"/>
          </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="退料仓库代码" prop="tlWarehouseCode">
              <el-input v-model="form.tlWarehouseCode" placeholder="请输入退料仓库代码"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="产品名称" prop="productName">
              <el-input v-model="form.productName" placeholder="请输入产品名称"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="退料仓库名称" prop="tlWarehouseName">
              <el-input v-model="form.tlWarehouseName" placeholder="请输入退料仓库名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="生产线简码" prop="proSimpleCode">
              <el-input v-model="form.proSimpleCode" placeholder="请输入生产线简码"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注"/>
            </el-form-item>
          </el-col>
        </el-row>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!--  <div class="el-upload__tip" slot="tip">
              <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
            </div>-->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                   @click="importTemplate">下载模板
          </el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listTechDetail, getTechDetail, delTechDetail, addTechDetail, updateTechDetail} from "@/api/base/techDetail";
import {getToken} from "@/utils/auth";

export default {
  name: "TechDetail",
  dicts: ['line_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工艺明细表格数据
      techDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        xh: null,
        gcCode: null,
        gcName: null,
        gwName: null,
        gwCode: null,
        specModel: null,
        lineType: null,
        lineCode: null,
        lineName: null,
        itemCode: null,
        itemName: null,
        bwlSpec: null,
        dtyl: null,
        djzl: null,
        djys: null,
        djrg: null,
        wlly: null,
        jggy: null,
        qgw: null,
        hgw: null,
        xlgw: null,
        ywsj: null,
        gwsx: null,
        gwsm: null,
        glmdd: null,
        glly: null,
        schcq: null,
        xbkdm: null,
        cpbm: null,
        xbkmc: null,
        sxckbm: null,
        sxckmc: null,
        sxdykz: null,
        ldWays: null,
        ldTime: null,
        ldAddress: null,
        pcWarehouseCode: null,
        pcWarehouseName: null,
        tlWarehouseCode: null,
        productName: null,
        tlWarehouseName: null,
        proSimpleCode: null,
      },
      upload: {
        // 是否显示弹出层（物料导入）
        open: false,
        // 弹出层标题（物料导入）
        title: "导入`techDetail`信息",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/base/techDetail/importData"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工艺明细列表 */
    getList() {
      this.loading = true;
      listTechDetail(this.queryParams).then(response => {
        console.log(response.rows,"response.rows")
        this.techDetailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        xh: null,
        gcCode: null,
        gcName: null,
        gwName: null,
        gwCode: null,
        specModel: null,
        lineType: null,
        lineCode: null,
        lineName: null,
        itemCode: null,
        itemName: null,
        bwlSpec: null,
        dtyl: null,
        djzl: null,
        djys: null,
        djrg: null,
        wlly: null,
        jggy: null,
        qgw: null,
        hgw: null,
        xlgw: null,
        ywsj: null,
        gwsx: null,
        gwsm: null,
        glmdd: null,
        glly: null,
        schcq: null,
        xbkdm: null,
        cpbm: null,
        xbkmc: null,
        sxckbm: null,
        sxckmc: null,
        sxdykz: null,
        ldWays: null,
        ldTime: null,
        ldAddress: null,
        pcWarehouseCode: null,
        pcWarehouseName: null,
        tlWarehouseCode: null,
        productName: null,
        tlWarehouseName: null,
        proSimpleCode: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工艺明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTechDetail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工艺明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTechDetail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTechDetail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除工艺明细编号为"' + ids + '"的数据项？').then(function () {
        return delTechDetail(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('base/techDetail/export', {
        ...this.queryParams
      }, `techDetail_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "工艺明细导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('base/techDetail/importTemplate', {}, `工艺明细模板.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", {dangerouslyUseHTMLString: true});
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
  }
};
</script>
